/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tankproject;

/**
 *
 * @author Thien
 */
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

class TankController implements KeyListener, MouseListener, MouseMotionListener {

    private Tank tank = null;
    private Turret turret = null;
    private TankView view = null;
    boolean key_right, key_left, key_down, key_up;

    public void keyTyped(KeyEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void keyPressed(KeyEvent e) {
        Sounds moving = new Sounds("moving1.wav");

        if (e.getKeyCode() == KeyEvent.VK_DOWN) {
            key_down = true;
            Timer timer = new Timer();
            TimerTask task = new TimerTask() {

                @Override
                public void run() {
                    tank.setX(tank.getX() - Math.sin(Math.toRadians(tank.rotationAngle)));
                    tank.setY(tank.getY() + Math.cos(Math.toRadians(tank.rotationAngle)));
                }
            };
            timer.scheduleAtFixedRate(task, 0, 100);
        }
        if (e.getKeyCode() == KeyEvent.VK_UP) {
            key_up = true;
            Timer timer = new Timer();
            TimerTask task = new TimerTask() {

                @Override
                public void run() {
                    tank.setX(tank.getX() + Math.sin(Math.toRadians(tank.rotationAngle)));
                    tank.setY(tank.getY() - Math.cos(Math.toRadians(tank.rotationAngle)));
                }
            };
            timer.scheduleAtFixedRate(task, 0, 100);
        }
        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            key_right = true;
            tank.rotationAngle += 3;
            moving.start();
        }
        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            key_left = true;
            tank.rotationAngle -= 3;
            moving.start();
        }

    }

    public void keyReleased(KeyEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseClicked(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mousePressed(MouseEvent e) {
        ArrayList bullets = new ArrayList();
        for (int w = 0; w < bullets.size(); w++) {
            //This is how to get a current element in an arrayList
            //similar to x[2] in a normal array
            Bullet m = (Bullet) bullets.get(w);//draw:
            if (m.getVisible() == true) {
                m.move();
            } else {
                bullets.remove(w);
            }
        }
    }

    public void mouseReleased(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseEntered(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseExited(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    /**
     * @return the tank
     */
    public Tank getTank() {
        return tank;
    }

    /**
     * @param tank the tank to set
     */
    public void setTank(Tank tank) {
        this.tank = tank;
    }

    /**
     * @return the view
     */
    public TankView getView() {
        return view;
    }

    /**
     * @param view the view to set
     */
    public void setView(TankView view) {
        this.view = view;
    }

    public void mouseDragged(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseMoved(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");

        double mX = e.getX();
        double mY = e.getY();
        double xD = (mX - (tank.getX() + 25));
        double yD = (mY - (tank.getY() + 50));
        turret.rotationAngle = (Math.atan2(yD, xD));
    }

    /**
     * @return the turret
     */
    public Turret getTurret() {
        return turret;
    }

    /**
     * @param turret the turret to set
     */
    public void setTurret(Turret turret) {
        this.turret = turret;
    }
}
